﻿#if !(NETFX_CORE || WINDOWS_PHONE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

namespace WindowsCommonStorage
{
    internal class NetFxStorageFile:CommonStorageFile
    {
        internal NetFxStorageFile()
        {
            this._file = null;
        }

        protected internal override async Task ProtectedCopyAndReplaceAsync(CommonStorageFile fileToReplace)
        {
            await Task.Factory.StartNew(() =>
            {
                
                File.Copy(this.Path, fileToReplace.Path, true);
            }).ConfigureAwait(false);
        }

        protected internal override string ProtectedPath
        {
            get { return this.EnvironmentFile.Directory.FullName; }
        }

        protected internal override string ProtectedName
        {
            get { return this.EnvironmentFile.Name; }
        }

        protected internal override CommonFileAttributes ProtectedAttributes
        {
            get { return CommonFileAttributesExchanger.Exchange(this.EnvironmentFile.Attributes); }
        }

        protected internal override DateTimeOffset ProtectedDateCreated
        {
            get { return this.EnvironmentFile.CreationTime; }
        }

        protected internal override async Task ProtectedRenameAsync(string desiredName)
        {
            await ProtectedRenameAsync(desiredName, CommonNameCollisionOption.FailIfExists).ConfigureAwait(false);
        }

        protected internal override async Task ProtectedRenameAsync(string desiredName, CommonNameCollisionOption option)
        {
            string path = this.EnvironmentFile.Directory.FullName + "\\" + desiredName;

            if (File.Exists(path))
            {
                switch (option)
                {
                    case CommonNameCollisionOption.FailIfExists:
                        throw new IOException("すでにファイルは存在します");

                    case CommonNameCollisionOption.GenerateUniqueName:
                        path = FileNameExtentions.GetUniqueName(path, 1);
                        break;
                        

                    case CommonNameCollisionOption.ReplaceExisting:
                        File.Delete(path);
                        break;
                }
            }


            await Task.Factory.StartNew(() =>
            {
                this.EnvironmentFile.MoveTo(this.Path + "\\" + desiredName);
            }).ConfigureAwait(false);
        }

        protected internal override async Task ProtectedDeleteAsync()
        {
            await Task.Factory.StartNew((sender) =>
            {
                this.EnvironmentFile.Delete();

            }, SynchronizationContext.Current).ConfigureAwait(false);
        }

        protected internal override bool ProtectedIsOfType(CommonStorageItemTypes itemType)
        {
            return itemType == CommonStorageItemTypes.File;
        }

        protected internal override FileInfo ProtectedEnvironmentFile
        {
            get { return _file; }
        }

        internal FileInfo _file;

        protected internal override async Task<Stream> ProtectedOpenStreamAsync()
        {
            return await Task.Factory.StartNew((sender) =>
            {
                return this.EnvironmentFile.Open(FileMode.Open);
            }, SynchronizationContext.Current).ConfigureAwait(false);
        }

        protected internal static async Task<CommonStorageFile> ProtectedGetFileFromPathAsync(string path)
        {
            return await Task.Factory.StartNew(() =>
            {
                return new NetFxStorageFile() { _file = new FileInfo(path) };
            });
        }
    }
}
#endif